﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace ConsoleTest
{
    class Program
    {
        static string appPath = Directory.GetCurrentDirectory().Replace(@"bin\Debug", string.Empty);


        static void Main(string[] args)
        {
            var image = Bitmap.FromFile(appPath + "test1.jpg");

            //var newImage = convert(image, appPath + "new.jpg", ImageFormat.Jpeg, 75);

            var newImage = ResizeImage(image, 128, 128);

            ImageCodecInfo jgpEncoder = GetEncoderInfo(ImageFormat.Jpeg);
            
           System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;

            EncoderParameters myEncoderParameters = new EncoderParameters(1);
            EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 75L);
            myEncoderParameters.Param[0] = myEncoderParameter;
            newImage.Save(appPath + "new.jpg", jgpEncoder, myEncoderParameters);

           

            //VaryQualityLevel();
        }

        private static void VaryQualityLevel()
        {
            // Get a bitmap.
            Bitmap bmp1 = new Bitmap(appPath + "Test.jpg");
            ImageCodecInfo jgpEncoder = GetEncoderInfo(ImageFormat.Jpeg);

            // Create an Encoder object based on the GUID
            // for the Quality parameter category.
            System.Drawing.Imaging.Encoder myEncoder =
                System.Drawing.Imaging.Encoder.Quality;

            // Create an EncoderParameters object.
            // An EncoderParameters object has an array of EncoderParameter
            // objects. In this case, there is only one
            // EncoderParameter object in the array.
            EncoderParameters myEncoderParameters = new EncoderParameters(1);

            EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 75L);
            myEncoderParameters.Param[0] = myEncoderParameter;
            bmp1.Save(appPath+ "TestPhotoQualityFifty.jpg", jgpEncoder, myEncoderParameters);

            myEncoderParameter = new EncoderParameter(myEncoder, 100L);
            myEncoderParameters.Param[0] = myEncoderParameter;
            bmp1.Save(appPath+ "TestPhotoQualityHundred.jpg", jgpEncoder, myEncoderParameters);

            // Save the bitmap as a JPG file with zero quality level compression.
            myEncoderParameter = new EncoderParameter(myEncoder, 0L);
            myEncoderParameters.Param[0] = myEncoderParameter;
            bmp1.Save(appPath + "TestPhotoQualityZero.jpg", jgpEncoder, myEncoderParameters);

        }





        private static ImageCodecInfo GetEncoderInfo(ImageFormat format)
        {

            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                {
                    return codec;
                }
            }
            return null;
        }



      


        public static Image ResizeImage(Image img, int width, int height)
        {
            var b = new Bitmap(width, height, PixelFormat.Format24bppRgb);

            using (Graphics g = Graphics.FromImage(b))
            {
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.CompositingQuality = CompositingQuality.HighQuality;

                g.DrawImage(img, 0, 0, width, height);
            }

            return b;
        }

        // compare with ImageResize, above, which has to load the bitmap from a file
        public static Bitmap scaleBitmap(Image Original, int newWidth)
        {
            int newHeight = Convert.ToInt32(Math.Round(Convert.ToDouble(
                ((double)newWidth / (double)Original.Width * (double)Original.Height))));

            Bitmap result = new Bitmap(newWidth, newHeight);
            Graphics gra = Graphics.FromImage(result);
            gra.SmoothingMode = SmoothingMode.HighQuality;
            gra.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gra.CompositingQuality = CompositingQuality.HighQuality;
            gra.SmoothingMode = SmoothingMode.HighQuality;

            gra.DrawImage(Original, 0, 0, newWidth, newHeight);
            Original.Dispose();
            Original = null;
            gra.Dispose();
            gra = null;
            return result;
        }
    }
}
